﻿<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">

      <div class="table-page-search-wrapper" v-if="hasPerm('DayReport:page')">
        <a-form layout="inline">
          <a-row :gutter="48">

            <a-col :md="8" :sm="24">
              <a-form-item label="日报日期">
                <a-date-picker style="width: 100%" placeholder="请选择" v-model="queryParam.dayName"
                  @change="onChangeDayName" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item v-model="queryParam.type" label="日报类型" has-feedback >
                <a-select style="width: 100%" placeholder="请选择日报类型">
                  
                  <a-select-option 
                  v-for="(item,index) in reportType"
                  :key="index"
                  :value="item.value"
                  @click="typeChange(item.value)">{{ item.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="数据大类">
                  <a-input v-model="queryParam.parentDataName" allow-clear placeholder="请输入上级数据名称" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="数据编码">
                  <a-input v-model="queryParam.dataCode" allow-clear placeholder="请输入编码" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="数据名称">
                  <a-input v-model="queryParam.dataName" allow-clear placeholder="请输入数据名称" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="数据状态">
                  <a-input v-model="queryParam.status" allow-clear placeholder="请输入状态" />
                </a-form-item>
              </a-col>

            </template>

            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button type="primary" :loading="btnLoading" @click="SyncPlatformHisReport">数据下载</a-button>
                <a-button type="primary" :loading="btnLoading">数据审核</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px"> {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table ref="table" 
      size="small"
      :columns="columns" 
      :data="loadData" 
      :alert="true" 
      :rowKey="(record) => record.id"
      :scroll="{ x: 1500, y: 600 }"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true">
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('DayReport:edit')" @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('DayReport:edit') & hasPerm('DayReport:delete')" />
          <a-popconfirm v-if="hasPerm('DayReport:delete')" placement="topRight" title="确认删除？"
            @confirm="() => DayReportDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import moment from 'moment'
import { DayReportPage, DayReportDelete, SyncPlatformHisReport } from '@/api/modular/main/DayReportManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
export default {
  components: {
    STable,
    addForm,
    editForm
  },
  data() {
    return {
      btnLoading: false,
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},
      reportType: [
        { name: "早报", value: "0" },
        { name: "午报", value: "1" },
        { name: "晚报", value: "2" },
      ],
      columns: [
        {
          title: '数据大类',
          align: 'center',
          dataIndex: 'parentDataName',
          fixed:"left",
          width:220,
          align:"left"
        },
        {
          title: '数据名称',
          align: 'center',
          dataIndex: 'dataName',
          align:"left",
          fixed:"left",
          width:220
        },
        {
          title: '挂号数量',
          align: 'center',
          dataIndex: 'dayNum',

          width:150,
        },
        {
          title: '金额',
          align: 'center',
          dataIndex: 'dataAmount',
          width:150,
        },
        {
          title: '当月累计数量',
          align: 'center',
          dataIndex: 'monthNum',
          width:150,
        },
        {
          title: '当月累计金额',
          align: 'center',
          dataIndex: 'monthAmount',
          width:150,
        },
        {
          title: '充值金额',
          align: 'center',
          dataIndex: 'rechargeAmount',
          width:150,
        },
        {
          title: '续费金额',
          align: 'center',
          dataIndex: 'reNewAmount',
          width:150,
        },
        {
          title: '退费金额',
          align: 'center',
          dataIndex: 'refundAmount',
          width:150,
        },
        {
          title: '合计金额',
          align: 'center',
          dataIndex: 'totalAmount',
          width:150,
        },
        {
          title: '日均数量',
          align: 'center',
          dataIndex: 'avgDayNum',
          width:150,
        },
        {
          title: '日均金额',
          align: 'center',
          dataIndex: 'avgDayAmount',
          width:150,
        },
        {
          title: '出院人数（日）',
          align: 'center',
          dataIndex: 'field01',
          width:150,
        },
        {
          title: '出院人数（月）',
          align: 'center',
          dataIndex: 'field02',
          width:150,
        },
        {
          title: '建卡人数',
          align: 'center',
          dataIndex: 'field03',
          width:150,
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          width:80
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return DayReportPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  created() {
    if (this.hasPerm('DayReport:edit') || this.hasPerm('DayReport:delete')) {
      this.columns.push({
        title: "操作",
        width: 120,
        minWidth:120,
        align:"center",
        dataIndex: 'action',
        fixed:"right",
        scopedSlots: { customRender: 'action' }
      })
    }
  },

  methods: {
    moment,
    /**
     * 查询参数组装
     */
    switchingDate() {

      const queryParamDayName = this.queryParam.dayName;
      if (queryParamDayName != null) {
        this.queryParam.dayName = moment(queryParamDayName).format('YYYY-MM-DD')
        if (queryParam.length < 1) {
          delete this.queryParam.dayName
        }
      }
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    formatDate(d) {
      return d ? moment(d).format('YYYY-MM-DD') : ''
    },
    onChangeDayName(date, dateString) {
      this.queryParam.dayName = dateString;
    },
    DayReportDelete(record) {
      DayReportDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    SyncPlatformHisReport() {
      let parameter = this.queryParam;// { dayName: this.queryParam.dayName, type: this.queryParam.type };
      console.log(parameter);
      this.btnLoading = true;
      SyncPlatformHisReport(parameter).then((res) => {
        this.btnLoading = false;
        if (res.success) {
          this.$message.success('HIS数据同步成功')
          this.$refs.table.refresh();
        } else {
          this.$message.error('HIS数据同步同步失败' + res.message) //
        }
        
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh();
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    typeChange(value) {
      this.queryParam.type = value;
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
